package com.biostime.terminal.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Element;

public class ArrayLists extends BaseElement {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2489883785686475503L;

	private static Log log = LogFactory.getLog(ArrayLists.class);
	
	protected static final String type = "arrayList";

	private String name = "";

	/**
	 * @return Returns the name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}	

	public boolean addElement(BaseElement baseXmlElement) {
		getElements().addElement(baseXmlElement);
		return true;
	}

	public boolean delElement(BaseElement baseXmlElement) {
		if (getElements().size() != 0){
			return getElements().removeElement(baseXmlElement);
		}
		return false;
	}
	
	public VO[] getVOs(){
		return (VO[]) getElements().toArray(new VO[0]);
	}
	
	/**
	 * @return 返回当前XML节点的Element对象
	 */
	public Element toElement() {
		Element list = new Element(type);
		if ((name != null) && (!name.trim().equals(""))) {
			list.setAttribute("name", name);
		}
		for (int i = 0; i < getElements().size(); i++) {
			list.addContent(((BaseElement)getElements().get(i)).toElement());
		}
		return list;
	}

	/**
	 * @return 返回当前XML节点的String对象，用于测试验证
	 */
	public String toString() {
		log.debug("[arrayList] name: " + getName());
		return XMLWriter.writeString(toElement());
	}
}